package com.xdxc.component.validation;


import com.xdxc.annotation.AnswerValidationRule;
import com.xdxc.model.dto.UserAnswerDto;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import static com.xdxc.utils.convertToLongList.convertToLongList;
@AnswerValidationRule(quType = 3)
@Component
public class JudgmentQuestionValidator implements AnswerValidator{
    @Override
    public boolean validate(UserAnswerDto dto) {
        // 1. 检查 DTO 是否为空、问题 ID 和答案类型是否有效
        if (dto == null || dto.getQuId() == null || dto.getAnswerType() == null) {
            return false;
        }

        // 2. 检查 answerIds 是否为空或超过 2 个
        List<?> rawAnswerIds = dto.getAnswerIds();
        if (rawAnswerIds == null || rawAnswerIds.isEmpty() || rawAnswerIds.size() > 2) {
            return false;
        }

        // 3. 检查 answerIds 是否为 List<Long>，如果不是则尝试转换
        try {
            List<Long> answerIds = convertToLongList(rawAnswerIds);
            return answerIds != null; // 转换成功才返回 true
        } catch (Exception e) {
            return false; // 转换失败返回 false
        }
    }
}
